package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SpecParamService specParamService;

    /**
     * 根据条件分页查询spu
     *
     * @param page       当前页码
     * @param rows       每页大小
     * @param brandId    品牌id
     * @param categoryId 分类id
     * @param id         商品的spuid
     * @param saleable   是否上架
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "saleable", required = false) Boolean saleable) {
        PageDTO<SpuDTO> spuDTOPageDTO = spuService.querySpuByPage(page, rows, brandId, categoryId, id, saleable);
        return ResponseEntity.ok().body(spuDTOPageDTO);
    }

    /**
     * 商品新增
     *
     * @param spuDTO 该商品的spu spu_detail sku
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> addSpu(@RequestBody SpuDTO spuDTO) {
        spuService.addSpu(spuDTO);
        return ResponseEntity.ok().build();
    }


    /**
     * 修改商品的上下架情况
     *
     * @param gid      商品spu id
     * @param saleable 上架或下架
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(
            @RequestParam("id") Long gid,
            @RequestParam("saleable") Boolean saleable) {
        spuService.updateSaleable(gid, saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id查询商品
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id) {
        return ResponseEntity.ok().body(spuService.querySpuById(id));
    }

    /**
     * 根据sku id集合批量查询sku
     *
     * @param ids sku id集合
     * @return 查询到的sku集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok().body(skuService.querySkuByIds(ids));

    }

    /**
     * 根据spuId查询sku集合
     *
     * @param sid spuID
     * @return sku集合
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long sid) {
        return ResponseEntity.ok().body(skuService.querySkuBySpuId(sid));
    }

    /**
     * 根据id查询商品详情
     *
     * @param id 商品id
     * @return 商品详情
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id) {
        return ResponseEntity.ok().body(spuDetailService.querySpuDetailById(id));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param id 商品id
     * @param searching 是否参与搜索
     * @return 规格参数集合
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecValuesByID(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching", required = false) Boolean searching) {
        return ResponseEntity.ok().body(spuService.querySpecValuesByID(id, searching));
    }

    /**
     * 根据商品id查询商品
     * @param id 商品id
     * @return spuDTO
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id) {
        return ResponseEntity.ok().body(spuService.queryGoodsById(id));
    }

    /**
     * 更新商品
     * @param spuDTO 更新的商品信息
     * @return 无
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 批量减库存
     * @param map skuId 以及 减少的库存量
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<String, Object> map) {
        // TODO:修改方法
        skuService.minusStock(map);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }













}
